<?php 
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model\Postaction as Postaction;


/*
*  帖子
*
 */
class Post extends Controller
{
	// 帖子内容
	public function index($id)
	{
		// 查询帖子内容
		$note = db('note')->where('id',$id)->find();
		// 二级分类查询
        $cate = db('cate')->field('id,catename,pid')->where('id', $note['cateid'])->find();
        // 一级分类 标题
        $first = db('cate')->field('id,catename')->where('id',$cate['pid'])->find();
		// 每一次进入访问量加1
		$note['scan'] = ($note['scan'] + 1);
		db('note')->where('id',$id)->update(['scan'=>$note['scan']]);
		// 查询用户信息
		$user = db('information')->where('userid', $note['uid'])->find();
		// 用户发表帖子数量
		$num = db('note')->where('uid',$note['uid'])->count('*');
		//转换时间
		$note['time'] = date('y-m-d h:i',$note['time']);
		// 评论
		$action = new Postaction();
		$hand = $action->handle($id);
		// var_dump($hand);die;
		$this->assign('note',$note);
		$this->assign('cate',$cate);
		$this->assign('first',$first);
		$this->assign('user',$user);
		$this->assign('hand',$hand);
		$this->assign('num',$num);

		return $this->fetch();
	}
	// 点赞(Fabulous)
	public function fabu()
	{
		$id = input('post.')['id'];
		$cool = db('note')->where('id',$id)->find()['cool'];
		$cool = $cool+1;
		db('note')->where('id',$id)->update(['cool'=>$cool]);
		return $cool;
	}
	// 踩
	public function fuck()
	{
		$id = input('post.')['id'];
		$fuck = db('note')->where('id',$id)->find()['fuck'];
		$fuck = $fuck+1;
		db('note')->where('id',$id)->update(['fuck'=>$fuck]);
		return $fuck;
	}
	// 发表帖子
	public function add($id)
	{
		// 帖子中的图片
		$file = request()->file('image');
		if ($file != null) {
			// 图片上传路径
			$info = $file->move(ROOT_PATH . 'public/static/index' . DS .'postimg');
			if ($info) {
				// 图片地址
				$img = $info->getSaveName();
				// 添加
				$data = ['uid'=>session('user')['id'],'pic'=>$img,'cateid'=>$id,'title'=>input('post.')['title'],'content'=>input('post.')['editorValue'],'time'=>time()];
				db('note')->insert($data);
				// 加积分
				$user = db('information')->where('userid',session('user')['id'])->find()['jifen'];
				db('information')->where('userid',session('user')['id'])->update(['jifen'=>($user+4)]);
			}
		}else{
			// 无图片发表
			$data = ['uid'=>session('user')['id'],'cateid'=>$id,'title'=>input('post.')['title'],'content'=>input('post.')['editorValue'],'time'=>time()];
			db('note')->insert($data);
			// 加积分
			$user = db('information')->where('userid',session('user')['id'])->find()['jifen'];
			db('information')->where('userid',session('user')['id'])->update(['jifen'=>($user+4)]);
		}

		return $this->success('发表成功',url('notelist/index',array('id'=>$id)));
	}
}